<?php

require_once 'database_engine/condition.php';

class UpdateCommandBuilder
{
    private $table;

    /** @var array */
    private $fieldValues;

    /** @var MultiCondition */
    private $rootCondition;

    /** @var \IConditionImp|null */
    private $conditionImp;

    /**
     * @param IConditionImp|null $conditionImp
     */
    public function __construct(IConditionImp $conditionImp = null)
    {
        $this->conditionImp = isset($conditionImp) ? $conditionImp : new GenericConditionImp();
        $this->table = null;
        $this->fieldValues = array();
        $this->rootCondition = new MultiCondition(MultiOperator::$And, array());
    }

    /**
     * @param string $tableName
     * @return void
     */
    public function SetTable($tableName)
    {
        $this->table = $tableName;
    }

    /**
     * @param string $fieldName
     * @param mixed $value
     * @return void
     */
    public function SetFieldValue(FieldName $fieldName, IExpression $value)
    {
        $this->fieldValues[] = array(
            'fieldName' => $fieldName,
            'value' => $value
        );
    }

    /**
     * @param IExpression $condition
     * @return void
     */
    public function AddCondition(IExpression $condition)
    {
        $this->rootCondition->Add($condition);
    }

    /**
     * @return string
     */
    public function GetSql()
    {
        $setSql = '';
        $conditionImp = $this->conditionImp;

        /** @var IExpression $value */
        array_walk($this->fieldValues, function($fieldNameAndValue) use (&$setSql, &$conditionImp)
        {
            /** @var IExpression $value .*/
            $value = $fieldNameAndValue['value'];
            /** @var FieldName $fieldName .*/
            $fieldName = $fieldNameAndValue['fieldName'];
            StringUtils::AddStr($setSql, $fieldName->AsSql($conditionImp) . ' = ' . $value->AsSql($conditionImp), ', ');
        });

        $result = '';
        StringUtils::AddStr($result, 'UPDATE');
        StringUtils::AddStr($result, $this->table, ' ');
        StringUtils::AddStr($result, $setSql, ' SET ');

        StringUtils::AddStr(
            $result,
            $this->rootCondition->AsSql($this->conditionImp),
            ' WHERE ');

        return $result;
    }
}

interface ICommand
{
    
}

class Command implements ICommand
{
    public function __construct()
    {
        
    }
}

?>